package com.neusmart.fs.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.baidu.mobstat.StatService;
import com.neusmart.fs.F;
import com.neusmart.fs.R;
import com.neusmart.fs.constants.API;
import com.neusmart.fs.model.MParam;
import com.neusmart.fs.model.UploadPic;

import java.util.ArrayList;
import java.util.List;

public class ChangeUserMassageCommitActivity extends DataLoadActivity implements View.OnClickListener {
    private Bundle bundle;
    private EditText phoneText;
    private int height,sex,weight,education_background,birthday_year,constellation;
    private String identity_no,province,graduate_school,weixin_id,personal_required,professional,phoneNum,characterization,city,country,name,edu_bg_photo;
    private String avatar;
    private List<String> mList;
    private ArrayList<UploadPic> photos;
    private String hhh;
    @Override
    protected int getLayoutResID() {
        return R.layout.activity_change_user_massage_commit;
    }

    @Override
    protected void init() {
        initView();
        loadExtraData();
        setListener();
    }
    private void loadExtraData() {
        bundle = getIntent().getExtras();
        if (bundle == null){
            return;
        }
        name=bundle.getString("userName");
        avatar = bundle.getString("avatar");
        photos = (ArrayList<UploadPic>)bundle.getSerializable("photo");
        mList=bundle.getStringArrayList("identity_photo");
        edu_bg_photo=bundle.getString("edu_bg_photo");
        weixin_id=bundle.getString("weixinId");
        height=Integer.parseInt(bundle.getString("height"));
        province=bundle.getString("province");
        graduate_school=bundle.getString("school");
        personal_required=bundle.getString("required");
        professional=bundle.getString("professional");
        characterization=bundle.getString("characterization");
        city=bundle.getString("city");
        country=bundle.getString("country");
        if ("男".equals(bundle.getString("sex"))){
            sex=1;
        }else if ("女".equals(bundle.getString("sex"))){
            sex=2;
        }
        weight=Integer.parseInt(bundle.getString("weight"));
        if ("硕士".equals(bundle.getString("educationBackground"))){
            education_background=1;
        }else if ("本科".equals(bundle.getString("educationBackground"))){
            education_background=0;
        }else if ("博士".equals(bundle.getString("educationBackground"))){
            education_background=2;
        }
        identity_no=bundle.getString("identityNo");
        birthday_year=Integer.parseInt(bundle.getString("birthday"));
        int length = getResources().getStringArray(R.array.constellation).length;
        for (int i=0;i<length;i++){
            if (bundle.getString("constellation").equals(getResources().getStringArray(R.array.constellation)[i])){
                constellation=i;
                return;
            }
        }

    }

    private void initView() {
        phoneText= (EditText) findViewById(R.id.phonenum_editText);
        mList=new ArrayList<>();
        //mList.add("http://fshome-qa.oss-cn-beijing.aliyuncs.com/identity/2016-12-23/c73fd0a0c8db11e6b28eddf861dd87de.jpg");

    }
    private void setListener() {
        int[] resIds = {R.id.my_data_btn_back,R.id.btn_change_usermassage_commit};
        for (int id : resIds)
            findViewById(id).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_data_btn_back:
                onBackPressed();
                break;
            case R.id.btn_change_usermassage_commit:
                //调用修改信息的接口
                phoneNum= phoneText.getText().toString();
                if (phoneNum.toCharArray().length==11){
                    phoneNum= phoneText.getText().toString();
                }else if (phoneNum.toCharArray().length>0 && phoneNum.toCharArray().length<11){
                    showToast("请输入正确的手机号");
                    return;
                }
                loadData(API.UPDATE_PROFILE, true);
                break;
            default:
                break;
        }
    }
    @Override
    protected void initParams(MParam param) {
        switch (param.getApi()) {
            case UPDATE_PROFILE:
                if (F.mUserInfo.getUser_status()!=0){//不是新用户
                    param.addParam("hight", height);
                param.addParam("sex", sex);
                param.addParam("identity_no", identity_no);
                param.addParam("province", province);
                param.addParam("graduate_school", graduate_school);
                param.addParam("weixin_id", weixin_id);
                param.addParam("weight", weight);
                param.addParam("education_background", education_background);
                param.addParam("personal_required", personal_required);
                param.addParam("professional", professional);
                param.addParam("characterization", characterization);
                param.addParam("birthday_year", birthday_year);
                param.addParam("constellation", constellation);
                param.addParam("city", city);
                param.addParam("country", country);
                param.addParam("name", name);
                if (edu_bg_photo!=null){//学历
                    param.addParam("edu_bg_photo", edu_bg_photo);
                }
                param.addParam("head_url",avatar);//头像
                param.addParam("photos",photos);//相册
                param.addParam("identity_photo", mList);//身份*/
                    param.addParam("phoneNum", phoneNum);
                    param.addParam("needUpdateStatus",true);//1
                }else {//新用户
                    param.addParam("phoneNum", phoneNum);
                    param.addParam("needUpdateStatus",true);//1
                }
                break;
            default:
                break;
        }
    }
    @Override
    protected void disposeResult(API api, String response) {
        if (response == null)
            return;
        switch (api) {
            case UPDATE_PROFILE:
                showToast("成功");

                StatService.onEvent(this, "updateInfo_api", "填写资料及修改资料统计",1);
                SharedPreferences setting = getSharedPreferences("test", 0);
                int level = setting.getInt("level", 3);
                if (level!=1){//1说明没有权限2说明只让看首页3说明都可以看
                    Bundle bundle=new Bundle();
                    switchActivity(MainActivity.class,bundle);
                }else if (level==1){//跳转到照片墙
                    switchActivity(AuditLoadingActivity.class,null);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        initSwipeBackFinish(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
